package cn.itcast.Service.Impl;

import cn.itcast.Service.HydropowerService;
import cn.itcast.feign.pojo.Check;
import cn.itcast.feign.pojo.Hydropower;
import cn.itcast.mapper.HydropowerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class HydropowerServiceImpl implements HydropowerService {
    @Autowired
    private HydropowerMapper hydropowerMapper;
//    @Autowired
//    private CheckClient checkClient;
    @Override
    public void insert(Hydropower hydropower) {
        Check check = new Check();//假数据
        check.setCheckId(1);//假数据
        check.setCheckinTime(LocalDateTime.of(2023,10,9,10,13,57));//假数据
        check.setCheckoutTime(LocalDateTime.of(2023,10,10,10,13,58));//假数据
        check.setState(2);//假数据
        check.setUserId(1);//假数据

        //Check check = checkClient.listById(hydropower.getCheckId());
        LocalDateTime checkinTime = check.getCheckinTime();
        LocalDateTime checkoutTime = check.getCheckoutTime();
        long begin = checkinTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long end = checkoutTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long twelve = TimeUnit.HOURS.toMillis(12);
        if (check.getState()==2){
            //退房
            long time = end-begin;
            double count = Math.ceil((double) time/twelve);
            String price = String.valueOf((int)count*10);
            hydropower.setPrice(price);
        }
        hydropower.setCheckId(check.getCheckId());
        hydropowerMapper.insert(hydropower);

    }
}
